package com.aode.sovo_miniprogram.controller;

import com.aode.sovo_miniprogram.pojo.Detail;
import com.aode.sovo_miniprogram.pojo.Firstclass;
import com.aode.sovo_miniprogram.service.DetailService;
import com.aode.sovo_miniprogram.service.FirstclassService;
import com.aode.sovo_miniprogram.utils.ResultMsgUtils;
import com.aode.sovo_miniprogram.pojo.Secondclass;
import com.aode.sovo_miniprogram.service.SecondclassService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/secondclass")
public class SecondclassController {
    @Autowired
    FirstclassService firstclassService;

    @Autowired
    SecondclassService secondclassService;

    @Autowired
    DetailService detailService;

    /*
    * 点击第一分类 根据第一个类的id
    * 判断是否存在二级分类
    *   存在：显示二级分类  ExistSecondClass(201,"第二分类"),
    *   不存在：
    *       判断：是否存在详细页
    *       存在：显示详细页  ExistDetail(202,"服务详细"),
    *       不存在：未完善
    * */
    @RequestMapping("/checkByClassId/{class_id}")
    public String checkSecondorDetail(@PathVariable("class_id") Integer id){
        ResultMsgUtils resultMsgUtils = new ResultMsgUtils();
        QueryWrapper<Secondclass> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Secondclass::getClass_id,id);
        List<Secondclass> secondclassList = secondclassService.list(queryWrapper);
        System.out.println(secondclassList);
        if (secondclassList.size()>0){
            resultMsgUtils.setExistSecond();
            resultMsgUtils.setData(secondclassList);
        }else{
            QueryWrapper<Detail> detailQueryWrapper = new QueryWrapper<>();
            detailQueryWrapper.lambda().eq(Detail::getClass_id,id);
            List<Detail> detailList = detailService.list(detailQueryWrapper);
            if (detailList.size()>0){
                resultMsgUtils.setExistDetail();
                resultMsgUtils.setData(detailList);
            }else {
                resultMsgUtils.setFail();
            }
        }
        return resultMsgUtils.getJsonString();
    }

    @RequestMapping("/selectDetailBySclassId/{sclass_id}")
    public String selectDetailfromSecond(@PathVariable("sclass_id")Integer id){
        ResultMsgUtils resultMsgUtils = new ResultMsgUtils();
        QueryWrapper<Detail> detailQueryWrapper = new QueryWrapper<>();
        detailQueryWrapper.lambda().eq(Detail::getSclass_id,id);
        List<Detail> list = detailService.list(detailQueryWrapper);
        if (list.size()>0){
            resultMsgUtils.setSuccess();
            resultMsgUtils.setData(list);
        }else
            resultMsgUtils.setFail();
        return resultMsgUtils.getJsonString();
    }

    /*
     * 更新或者保存第二分类
     * 如果id 是空的 则是保存
     * 如果id 存在   则是更新
     *        更新或者保存前必须看一级类是否存在
     *         存在：
     *              判断一级类的子类是否存在详细
     *                  不存在
     *                         执行更新或者保存
     *                              成功
     *                              失败
     *                  存在 失败
     *         不存在 失败
     * */
    @RequestMapping("/addOrUpdate")
    public String addSecondclass(Secondclass secondclass){
        ResultMsgUtils resultMsgUtils = new ResultMsgUtils();
        QueryWrapper<Firstclass> firstclassQueryWrapper = new QueryWrapper<>();
        firstclassQueryWrapper.lambda().eq(Firstclass::getClass_id,secondclass.getClass_id());
        List<Firstclass> list = firstclassService.list(firstclassQueryWrapper);
        System.out.println(list+" "+secondclass.getClass_id());
        if (list.size()>0){
            QueryWrapper<Detail> detailQueryWrapper = new QueryWrapper<>();
            detailQueryWrapper.lambda().eq(Detail::getClass_id,secondclass.getClass_id());
            List<Detail> detailList = detailService.list(detailQueryWrapper);
            if (detailList.size()==0){
                if (secondclassService.saveOrUpdate(secondclass)){
                    resultMsgUtils.setSuccess();
                    resultMsgUtils.setData(secondclass);
                }else {
                    resultMsgUtils.setFail();
                }
            }else
                resultMsgUtils.setFail();
        }else
            resultMsgUtils.setFail();
        return resultMsgUtils.getJsonString();
    }

    @RequestMapping("/del/{sclass_id}")
    public String delSecondclass(@PathVariable("sclass_id")Integer id){
        ResultMsgUtils resultMsgUtils = new ResultMsgUtils();
        Map<String,Object> map = new HashMap<>();
        map.put("sclass_id",id);
        if (secondclassService.removeByMap(map)){
            resultMsgUtils.setSuccess();
        }else
            resultMsgUtils.setFail();
        return resultMsgUtils.getJsonString();
    }

}
